<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="任务编号" prop="taskNo">
        <el-input
          v-model="queryParams.taskNo"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参训单位" prop="deptId">
        <el-select
          v-model="queryParams.deptId"
          placeholder="参训单位"
          clearable
        >
          <el-option
            v-for="item in deptOptions"
            :key="item.deptId"
            :label="item.deptName"
            :value="item.deptId"
            :disabled="item.status == 1"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="提交时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['task:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      ref="tables"
      v-loading="loading"
      :data="taskList"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort" 
      @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="taskId" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column
        label="任务编号"
        width="170"
        align="center"
        prop="taskNo"
        sortable="custom" :sort-orders="['descending', 'ascending']"
      />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="成绩" align="center" prop="grade"  sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="时长" align="center" prop="durationTime" sortable="custom" :sort-orders="['descending', 'ascending']" />
      <el-table-column
        label="参训单位"
        align="center"
        prop="deptName"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="视频" align="center" prop="url">
        <template slot-scope="scope">
          <div v-if="scope.row.url == null">
            <img
              src="@/assets/images/video_gray.png"
              alt="video"
              width="30"
            />
          </div>
          <div v-if="scope.row.url != null">
            <a :href="scope.row.url" target="_blank">
              <img
                src="@/assets/images/video_blue.png"
                alt="video"
                width="30"
              />
            </a>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="提交时间"
        align="center"
        prop="submitTime"
        width="180"
        sortable="custom" :sort-orders="['descending', 'ascending']"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.submitTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-row>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['evaluate:query']"
              >详细</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['evaluate:remove']"
              >删除</el-button
            >
          </el-row>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详细 -->
    <el-dialog
      :title="title"
      :visible.sync="taskView"
      width="900px"
      append-to-body
    >
      <el-form ref="evaluate" :model="evaluate" label-width="100px" size="mini">
        <el-form-item label="基本信息："></el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务编号：">
              <span>{{ evaluate.taskNo }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务名称：">
              <span>{{ evaluate.taskName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="视频：">
              <span></span>
              <div v-if="evaluate.url == null">
                <img
                  src="@/assets/images/video_gray.png"
                  alt="video"
                  style="width: 30px"
                />
              </div>
              <div v-if="evaluate.url != null">
                <a :href="evaluate.url" target="_blank">
                  <img
                    src="@/assets/images/video_blue.png"
                    alt="video"
                    style="width: 30px"
                  />
                </a>
              </div>
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="成绩：">
              <span>{{ evaluate.grade }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
           <el-col :span="12">
            <el-form-item label="时长：">
              <span>{{ evaluate.durationTime }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提交时间：">
              <span>{{
                parseTime(evaluate.submitTime, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注：" :show-overflow-tooltip="true">
              <span>{{ evaluate.remark }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <!--人员信息-->
        <el-form-item label="人员信息："></el-form-item>
        <el-table v-loading="loading" :data="personlTaskList">
          <el-table-column label="ID" align="center" prop="id" />
          <el-table-column label="姓名" align="center" prop="nickName" />
          <el-table-column label="单位" align="center" prop="deptName" />
          <el-table-column label="岗位" align="center" prop="postName" />
          <el-table-column label="个人成绩" align="center" prop="grade" sortable="custom" :sort-orders="['descending', 'ascending']" />
          <el-table-column label="视频" align="center" prop="url">
            <template slot-scope="scope">
              <div v-if="scope.row.url == null">
                <img
                  src="@/assets/images/video_gray.png"
                  alt="video"
                  style="width: 30px"
                />
              </div>
              <div v-if="scope.row.url != null">
                <a :href="scope.row.url" target="_blank">
                  <img
                    src="@/assets/images/video_blue.png"
                    alt="video"
                    style="width: 30px"
                  />
                </a>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="taskTotal > 0"
          :total="taskTotal"
          :page.sync="taskParams.pageNum"
          :limit.sync="taskParams.pageSize"
          @pagination="getPersonlTaskList"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="taskView = false">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listTask,getTask,delTask} from "@/api/evaluate/evaluate";
import { listPersonlTask} from "@/api/task/personlTask";
import { listDept } from "@/api/system/dept";

export default {
  name: "Evaluate",
  data() {
    return {
      //新增
      add: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务表格数据
      taskList: [],
      // 单位选项
      deptOptions: [],
      // 用户任务表格数据
      personlTaskList: [],
      //用户任务总数
      taskTotal: 0,
      // 弹出层标题
      title: "",
      // 日期范围
      dateRange: [],
      // 是否显示弹出层
      open: false,
      //是否弹出详情
      taskView: false,
      //任务编号
      taskNo: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        taskNo: null,
        taskName: null,
        nickName: null,
        deptId: null,
        status: null,
        submitTime: null,
      },
      // 默认排序
      defaultSort: {prop: 'submitTime', order: 'descending'},
      // 任务记录查询参数
      taskParams: {
        pageNum: 1,
        pageSize: 10,
        taskNo: undefined,
        userId: undefined,
      },
      // 表单参数
      evaluate: {},
    };
  },
  created() {
    this.getList();
    this.getListDept();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      listTask(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.taskList = response.rows;
          this.total = parseInt(response.total);
          this.loading = false;
        }
      );
    },

    /** 查询单位列表 */
    getListDept() {
      listDept({pageSize: 1000}).then((response) => {
        this.deptOptions = response.data;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.taskId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },

    /** 详细按钮操作 */
    handleView(row) {
      this.taskNo = row.taskNo;
      this.taskView = true;
      const taskId = row.taskId;
      getTask(taskId).then((response) => {
        this.evaluate = response.data;
        this.title = "考核详情";
      });

      //人员信息
      this.getPersonlTaskList();
    },

    /** 查询用户任务列表 */
    getPersonlTaskList() {
      this.taskParams.taskNo = this.taskNo;
      listPersonlTask(this.taskParams).then((response) => {
        this.personlTaskList = response.rows;
        this.taskTotal = parseInt(response.total);
      });
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const taskIds = row.taskId || this.ids;
      this.$modal
        .confirm('是否确认删除任务编号为"' + taskIds + '"的数据项？')
        .then(function () {
          return delTask(taskIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>